package yxy.game.pm2.module.player;

import org.joda.time.DateTime;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.PlayerRankModule;
import yxy.game.pm2.bean.module.rank.RankInfo;

import java.util.Collection;

public class RankPlayerLevel extends PlayerRankModule {

	public RankPlayerLevel(AreaBean areaBean) {
		super(areaBean);
	}

	@Override
	public String rankName() {
		return PlayerRankModule.getAllRankNames().get(7);
	}
	
	@Override
	protected boolean reconsitution() {
		return false;
	}

	@Override
	protected long valuePlastic(long value) {
		return value * 10000000000000L + (10000000000000L - DateTime.now().getMillis());// 13位时间戳长度
	}

	@Override
	protected long valueRestore(long score) {
		return score / 10000000000000L;
	}

	static public void reset() {
		redis().client().getKeys().deleteByPattern("AREA:*:RANK:LEVEL");

		Collection<PlayerBean> all = PlayerBean.all();

		for (PlayerBean bean : all) {
			String pCode = bean.getCode();
			AreaBean areaBean = AreaModule.area(bean);
			if (bean.getPower() > 0) {
				new RankPlayerLevel(areaBean).put(pCode, bean.getLevel());
			}
		}
	}

	//重置某个区
	static public void reset(int areaId) {
//		redis().client().getKeys().deleteByPattern("AREA:*:RANK:LEVEL");
//
//		Collection<PlayerBean> all = PlayerBean.all();
//
//		for (PlayerBean bean : all) {
//			String pCode = bean.getCode();
//			AreaBean areaBean = AreaModule.area(bean);
//			if (bean.getPower() > 0) {
//				new RankPlayerLevel(areaBean).put(pCode, bean.getLevel());
//			}
//		}
	}

	public void clear() {
		try {
			touchBucket(key()).delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static public RankPlayerLevel getRank(String playerCode) {
		AreaBean areaBean = AreaModule.area(playerCode);
		return new RankPlayerLevel(areaBean);
	}
}
